
/*
vgFabric.cc
This file is part of opengl-vrml-assembly.
Copyright (C) Rafal Pielech, rafal.pielech@gmail.com

opengl-vrml-assembly is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

opengl-vrml-assembly is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with opengl-vrml-assembly.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <windows.h>
#include <gl/gl.h>
#include <vgDebug.h>
#include <vgFabric.h>

vgFabric::vgFabric(void)
{
  VG_DEBUG
  specularProp[0] = 0.9f;
  specularProp[1] = 0.9f;
  specularProp[2] = 0.9f;
  specularProp[3] = 1.0f;
  
  emissionProp[0] = 0.1f;
  emissionProp[1] = 0.1f;
  emissionProp[2] = 0.1f;
  emissionProp[3] = 1.0f;
  
  shininessProp   = 50.0f;
}

void
vgFabric::operator()(const float r,
                     const float g,
                     const float b,
                     const float a)
{
  VG_DEBUG
  #define CLAMP(x) ((x>1.0)?(1.0):((x<0.0)?(0.0):(x)))
  colour[0] = CLAMP(r);
  colour[1] = CLAMP(g);
  colour[2] = CLAMP(b);
  colour[3] = CLAMP(a);
  #undef CLAMP
  
  #define QUALIFY(x) ((x>0.0)?(x):(0.0))
  ambientProp[0] = QUALIFY(colour[0] - 0.4);
  ambientProp[1] = QUALIFY(colour[1] - 0.4);
  ambientProp[2] = QUALIFY(colour[2] - 0.4);
  ambientProp[3] = QUALIFY(colour[3] - 0.4);
  #undef QUALIFY
  
  diffuseProp[0] = colour[0];
  diffuseProp[1] = colour[1];
  diffuseProp[2] = colour[2];
  diffuseProp[3] = colour[3];
  
  glMaterialfv(GL_FRONT, GL_AMBIENT  , ambientProp);
  glMaterialfv(GL_FRONT, GL_DIFFUSE  , diffuseProp);
  glMaterialfv(GL_FRONT, GL_SPECULAR , specularProp);
  glMaterialf (GL_FRONT, GL_SHININESS, shininessProp);
  glMaterialfv(GL_FRONT, GL_EMISSION , emissionProp);
}

vgFabric::~vgFabric(void)
{
  VG_DEBUG
}
